package repo

import (
	model "micro-oauth/datamodels"
	"micro-oauth/datamodels/enum"
)

func (s *ServiceImpl) AddClient(client *model.Client) error {
	return s.db.Create(client).Error
}

func (s *ServiceImpl) GetClientById(clientId string) *model.Client {
	var client model.Client
	err := s.db.Where("client_id = ?", clientId).First(&client).Error
	if err != nil {
		return nil
	}
	return &client
}
func (s *ServiceImpl) GetPosClientByTerminalId(terminalId uint64) *model.Client {
	var client model.Client
	err := s.db.Where("terminal_id = ? and type = ?", terminalId, enum.PosClient).First(&client).Error
	if err != nil {
		return nil
	}
	return &client
}

func (s *ServiceImpl) GetClientByPrimaryId(clientId uint64) *model.Client {
	var client model.Client
	err := s.db.Where("id = ?", clientId).First(&client).Error
	if err != nil {
		return nil
	}
	return &client
}

func (s *ServiceImpl) UpdateClient(client *model.Client) error {
	return s.db.Save(client).Error
}

func (s *ServiceImpl) DeleteClient(clientId uint64) error {
	return s.db.Unscoped().Where(
		"id = ?", clientId,
	).Delete(&model.Client{}).Error
}
